Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 'use client';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { VideoUploadField } from '@/components/ui/video-upload-field';
import { Film, Baby, Upload } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { contentService } from '@/services';
import { ContentType } from '@/types';
import { toast } from 'sonner';
import i18n from '@/lib/i18n';
// Manual movie schema - allows pending upload marker or valid URL
const manualMovieSchema = z.object({
title: z.string().min(1, i18n.t('manualSeries.titleRequired')),
description: z.string().optional(),
year: z.number().min(1900).max(new Date().getFullYear() + 5).optional(),
poster_url: z.string().url(i18n.t('errors.invalidData')).optional().or(z.literal('')),
backdrop_url: z.string().url(i18n.t('errors.invalidData')).optional().or(z.literal('')),
video_url: z.string().min(1, i18n.t('seriesManagement.videoUrlRequired')),
active: z.boolean()});
type ManualMovieData = z.infer<typeof manualMovieSchema>;
interface ManualMovieFormProps {
contentType: ContentType.VOD | ContentType.KIDS;
open: boolean;
onOpenChange: (open: boolean) => void;
onClose: () => void;
}
export default function ManualMovieForm({
contentType,
open,
onOpenChange,
onClose
}: ManualMovieFormProps) {
const queryClient = useQueryClient();
const { t } = useTranslation();
const [pendingUploadFile, setPendingUploadFile] = useState<File | null>(null);
const form = useForm<ManualMovieData>({
resolver: zodResolver(manualMovieSchema),
defaultValues: {
title: '',
description: '',
year: undefined,
poster_url: '',
backdrop_url: '',
video_url: '',
active: true}});
// Create content mutation
const createContentMutation = useMutation({
mutationFn: async (data: ManualMovieData) => {
const result = await contentService.createContent({
...data,
type: contentType});
if (result.success) {
return result.data;
}
throw new Error(result.error.details);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['content', contentType] });
toast.success(t('common.success'));
handleClose();
},
onError: (error: Error) => {
toast.error(t('notifications.content.createFailed', { message: error.message }));
}});
const handleClose = () => {
onClose();
form.reset();
setPendingUploadFile(null);
createContentMutation.reset();
};
const handleSubmit = async (data: ManualMovieData) => {
// If there's a pending upload, we need to create content first then upload
if (pendingUploadFile && data.video_url.startsWith('[Pending Upload]')) {
try {
// Create content with a placeholder
const result = await contentService.createContent({
...data,
video_url: '', // Will be updated after upload
type: contentType});
if (!result.success || !result.data) {
throw new Error(result.error?.details || t('common.serverError'));
}
// Upload the video file
const uploadResult = await contentService.uploadContentVideo(
result.data.id,
pendingUploadFile
);
if (!uploadResult.success) {
throw new Error(uploadResult.error?.details || t('common.serverError'));
}
queryClient.invalidateQueries({ queryKey: ['content', contentType] });
toast.success(t('common.success'));
handleClose();
} catch (error) {
toast.error(t('notifications.content.createFailed', {
message: error instanceof Error ? error.message : 'Unknown error'
}));
}
} else {
// Normal URL-based creation
createContentMutation.mutate(data);
}
};
const getContentTypeInfo = () => {
switch (contentType) {
case ContentType.VOD:
return {
icon: Film,
title: t('contentType.movie'),
description: t('createContent.manualAlert'),
color: 'bg-blue-500'};
case ContentType.KIDS:
return {
icon: Baby,
title: t('contentType.kids'),
description: t('createContent.manualAlert'),
color: 'bg-green-500'};
default:
return {
icon: Film,
title: t('contentType.movie'),
description: t('createContent.manualAlert'),
color: 'bg-gray-500'};
}
};
const getContentTypeLabel = () => {
return contentType === ContentType.VOD ? t('contentType.movie') : t('contentType.kids');
};
const contentInfo = getContentTypeInfo();
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="!max-w-[65vw] !w-[65vw] max-h-[75vh] overflow-y-auto sm:!max-w-[65vw] md:!max-w-[65vw] lg:!max-w-[65vw]" style={{ width: '65vw', maxWidth: '65vw' }}>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<contentInfo.icon className="h-5 w-5" />
{t('createContent.addNew', { type: contentInfo.title })}
</DialogTitle>
<DialogDescription>
{contentInfo.description}
</DialogDescription>
</DialogHeader>
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Upload className="h-4 w-4" />
{t('common.details')}
</CardTitle>
<CardDescription>
{t('createContent.manualAlert')}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Title */}
<div>
<Label htmlFor="title">{t('createContent.labels.title')} *</Label>
<Input
id="title"
{...form.register('title')}
placeholder={t('createContent.form.titlePlaceholder')}
/>
{form.formState.errors.title && (
<p className="text-sm text-red-600 mt-1">
{form.formState.errors.title.message}
</p>
)}
</div>
{/* Description */}
<div>
<Label htmlFor="description">{t('createContent.labels.description')}</Label>
<Textarea
id="description"
{...form.register('description')}
placeholder={t('createContent.form.descriptionPlaceholder')}
rows={3}
/>
</div>
{/* Year */}
<div>
<Label htmlFor="year">{t('createContent.labels.year')}</Label>
<Input
id="year"
type="number"
{...form.register('year', { valueAsNumber: true })}
placeholder={t('createContent.form.yearPlaceholder')}
/>
</div>
{/* Poster URL */}
<div>
<Label htmlFor="poster_url">{t('createContent.labels.posterUrl')}</Label>
<Input
id="poster_url"
{...form.register('poster_url')}
placeholder={t('createContent.form.posterUrlPlaceholder')}
/>
{form.formState.errors.poster_url && (
<p className="text-sm text-red-600 mt-1">
{form.formState.errors.poster_url.message}
</p>
)}
</div>
{/* Backdrop URL */}
<div>
<Label htmlFor="backdrop_url">{t('createContent.labels.backdropUrl')}</Label>
<Input
id="backdrop_url"
{...form.register('backdrop_url')}
placeholder={t('createContent.form.backdropUrlPlaceholder')}
/>
{form.formState.errors.backdrop_url && (
<p className="text-sm text-red-600 mt-1">
{form.formState.errors.backdrop_url.message}
</p>
)}
</div>
{/* Video URL or Upload */}
<div>
<Label htmlFor="video_url">{t('createContent.labels.videoUrl')} *</Label>
<VideoUploadField
value={form.watch('video_url')}
onChange={(value) => {
form.setValue('video_url', value);
// Check if this is a file selection
if (value.startsWith('[Pending Upload]')) {
// Keep track that we have a pending file
}
}}
onUpload={async (file) => {
// Store file for later upload after content creation
setPendingUploadFile(file);
return `[Pending Upload] ${file.name}`;
}}
disabled={createContentMutation.isPending}
placeholder={t('createContent.form.videoUrlPlaceholder')}
error={form.formState.errors.video_url?.message}
/>
</div>
{/* Active Status */}
<div className="flex items-center space-x-2">
<input
type="checkbox"
id="active"
{...form.register('active')}
className="rounded"
/>
<Label htmlFor="active">{t('common.active')}</Label>
</div>
</CardContent>
</Card>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={handleClose}
disabled={createContentMutation.isPending}
>
{t('common.cancel')}
</Button>
<Button
type="submit"
disabled={createContentMutation.isPending}
>
{createContentMutation.isPending ? t('createContent.creating') : t('createContent.createButton', { type: getContentTypeLabel() })}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}
|